<?php 

/**
* 
*/
class WordModel extends RelationModel
{
    protected $_link = array(
        'example' => array(
            'mapping_type' => HAS_MANY,  // 关系模式
            'class_name'   => 'example', // 关联的模型类名
            'foreign_key'  => 'w_id',    // 关联的外键名称
            'mapping_fields' => 'name, definition',
            'mapping_order' => 'name',
            ),

        'in' => array(
            'mapping_type' => BELONGS_TO,
            'class_name'   => 'in',
            'foreign_key'  => 'i_id',
            // 'as_fields'    => 'in',
            // 'mapping_fields' => 'title, abstract',
            ),
        );

    // 插入一个单词
    // 返回值： 0 => success
    //          1 => word repeat
    //          2 => failed
    public function insertWord($data)
    {
        if ($this->isWordExist($data['name'])) {
            return 1;
        } else {
            if (M('Word')->add($data))
                return 0;
            else
                return 2;
        }
    }

    private function isWordExist($name)
    {
        return M('Word')->where('name=' . $name)->select();
    }

    public function delWordByid(int $w_id)
    {
        if (M('Example')->where('w_id=' . $w_id)->delete() && 
            M('Word')->where('w_id=' . $w_id)->delete()) {
            return true;
        } else {
            return false;
        }
        

    }

}

 ?>